home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-6.z / elisp-6 (.txt)
GNU Info File  |  1998-05-26  |  48KB  |  938 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Association Lists,  Prev: Sets And Lists,  Up: Lists
  32. Association Lists
  33. =================
  34.    An "association list", or "alist" for short, records a mapping from
  35. keys to values.  It is a list of cons cells called "associations": the
  36. CAR of each cell is the "key", and the CDR is the "associated value".(1)
  37.    Here is an example of an alist.  The key `pine' is associated with
  38. the value `cones'; the key `oak' is associated with `acorns'; and the
  39. key `maple' is associated with `seeds'.
  40.      '((pine . cones)
  41.        (oak . acorns)
  42.        (maple . seeds))
  43.    The associated values in an alist may be any Lisp objects; so may the
  44. keys.  For example, in the following alist, the symbol `a' is
  45. associated with the number `1', and the string `"b"' is associated with
  46. the *list* `(2 3)', which is the CDR of the alist element:
  47.      ((a . 1) ("b" 2 3))
  48.    Sometimes it is better to design an alist to store the associated
  49. value in the CAR of the CDR of the element.  Here is an example:
  50.      '((rose red) (lily white) (buttercup yellow))
  51. Here we regard `red' as the value associated with `rose'.  One
  52. advantage of this method is that you can store other related
  53. information--even a list of other items--in the CDR of the CDR.  One
  54. disadvantage is that you cannot use `rassq' (see below) to find the
  55. element containing a given value.  When neither of these considerations
  56. is important, the choice is a matter of taste, as long as you are
  57. consistent about it for any given alist.
  58.    Note that the same alist shown above could be regarded as having the
  59. associated value in the CDR of the element; the value associated with
  60. `rose' would be the list `(red)'.
  61.    Association lists are often used to record information that you might
  62. otherwise keep on a stack, since new associations may be added easily to
  63. the front of the list.  When searching an association list for an
  64. association with a given key, the first one found is returned, if there
  65. is more than one.
  66.    In Emacs Lisp, it is *not* an error if an element of an association
  67. list is not a cons cell.  The alist search functions simply ignore such
  68. elements.  Many other versions of Lisp signal errors in such cases.
  69.    Note that property lists are similar to association lists in several
  70. respects.  A property list behaves like an association list in which
  71. each key can occur only once.  *Note Property Lists::, for a comparison
  72. of property lists and association lists.
  73.  - Function: assoc KEY ALIST
  74.      This function returns the first association for KEY in ALIST.  It
  75.      compares KEY against the alist elements using `equal' (*note
  76.      Equality Predicates::.).  It returns `nil' if no association in
  77.      ALIST has a CAR `equal' to KEY.  For example:
  78.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  79.                => ((pine . cones) (oak . acorns) (maple . seeds))
  80.           (assoc 'oak trees)
  81.                => (oak . acorns)
  82.           (cdr (assoc 'oak trees))
  83.                => acorns
  84.           (assoc 'birch trees)
  85.                => nil
  86.      Here is another example, in which the keys and values are not
  87.      symbols:
  88.           (setq needles-per-cluster
  89.                 '((2 "Austrian Pine" "Red Pine")
  90.                   (3 "Pitch Pine")
  91.                   (5 "White Pine")))
  92.           
  93.           (cdr (assoc 3 needles-per-cluster))
  94.                => ("Pitch Pine")
  95.           (cdr (assoc 2 needles-per-cluster))
  96.                => ("Austrian Pine" "Red Pine")
  97.  - Function: rassoc VALUE ALIST
  98.      This function returns the first association with value VALUE in
  99.      ALIST.  It returns `nil' if no association in ALIST has a CDR
  100.      `equal' to VALUE.
  101.      `rassoc' is like `assoc' except that it compares the CDR of each
  102.      ALIST association instead of the CAR.  You can think of this as
  103.      "reverse `assoc'", finding the key for a given value.
  104.  - Function: assq KEY ALIST
  105.      This function is like `assoc' in that it returns the first
  106.      association for KEY in ALIST, but it makes the comparison using
  107.      `eq' instead of `equal'.  `assq' returns `nil' if no association
  108.      in ALIST has a CAR `eq' to KEY.  This function is used more often
  109.      than `assoc', since `eq' is faster than `equal' and most alists
  110.      use symbols as keys.  *Note Equality Predicates::.
  111.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  112.                => ((pine . cones) (oak . acorns) (maple . seeds))
  113.           (assq 'pine trees)
  114.                => (pine . cones)
  115.      On the other hand, `assq' is not usually useful in alists where the
  116.      keys may not be symbols:
  117.           (setq leaves
  118.                 '(("simple leaves" . oak)
  119.                   ("compound leaves" . horsechestnut)))
  120.           
  121.           (assq "simple leaves" leaves)
  122.                => nil
  123.           (assoc "simple leaves" leaves)
  124.                => ("simple leaves" . oak)
  125.  - Function: rassq VALUE ALIST
  126.      This function returns the first association with value VALUE in
  127.      ALIST.  It returns `nil' if no association in ALIST has a CDR `eq'
  128.      to VALUE.
  129.      `rassq' is like `assq' except that it compares the CDR of each
  130.      ALIST association instead of the CAR.  You can think of this as
  131.      "reverse `assq'", finding the key for a given value.
  132.      For example:
  133.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  134.           
  135.           (rassq 'acorns trees)
  136.                => (oak . acorns)
  137.           (rassq 'spores trees)
  138.                => nil
  139.      Note that `rassq' cannot search for a value stored in the CAR of
  140.      the CDR of an element:
  141.           (setq colors '((rose red) (lily white) (buttercup yellow)))
  142.           
  143.           (rassq 'white colors)
  144.                => nil
  145.      In this case, the CDR of the association `(lily white)' is not the
  146.      symbol `white', but rather the list `(white)'.  This becomes
  147.      clearer if the association is written in dotted pair notation:
  148.           (lily white) == (lily . (white))
  149.  - Function: copy-alist ALIST
  150.      This function returns a two-level deep copy of ALIST: it creates a
  151.      new copy of each association, so that you can alter the
  152.      associations of the new alist without changing the old one.
  153.           (setq needles-per-cluster
  154.                 '((2 . ("Austrian Pine" "Red Pine"))
  155.                   (3 . ("Pitch Pine"))
  156.                   (5 . ("White Pine"))))
  157.           =>
  158.           ((2 "Austrian Pine" "Red Pine")
  159.            (3 "Pitch Pine")
  160.            (5 "White Pine"))
  161.           
  162.           (setq copy (copy-alist needles-per-cluster))
  163.           =>
  164.           ((2 "Austrian Pine" "Red Pine")
  165.            (3 "Pitch Pine")
  166.            (5 "White Pine"))
  167.           
  168.           (eq needles-per-cluster copy)
  169.                => nil
  170.           (equal needles-per-cluster copy)
  171.                => t
  172.           (eq (car needles-per-cluster) (car copy))
  173.                => nil
  174.           (cdr (car (cdr needles-per-cluster)))
  175.                => ("Pitch Pine")
  176.           (eq (cdr (car (cdr needles-per-cluster)))
  177.               (cdr (car (cdr copy))))
  178.                => t
  179.      This example shows how `copy-alist' makes it possible to change
  180.      the associations of one copy without affecting the other:
  181.           (setcdr (assq 3 copy) '("Martian Vacuum Pine"))
  182.           (cdr (assq 3 needles-per-cluster))
  183.                => ("Pitch Pine")
  184.    ---------- Footnotes ----------
  185.    (1)  This usage of "key" is not related to the term "key sequence";
  186. it means a value used to look up an item in a table.  In this case, the
  187. table is the alist, and the alist associations are the items.
  188. File: elisp,  Node: Sequences Arrays Vectors,  Next: Symbols,  Prev: Lists,  Up: Top
  189. Sequences, Arrays, and Vectors
  190. ******************************
  191.    Recall that the "sequence" type is the union of three other Lisp
  192. types: lists, vectors, and strings.  In other words, any list is a
  193. sequence, any vector is a sequence, and any string is a sequence.  The
  194. common property that all sequences have is that each is an ordered
  195. collection of elements.
  196.    An "array" is a single primitive object that has a slot for each
  197. elements.  All the elements are accessible in constant time, but the
  198. length of an existing array cannot be changed.  Strings and vectors are
  199. the two types of arrays.
  200.    A list is a sequence of elements, but it is not a single primitive
  201. object; it is made of cons cells, one cell per element.  Finding the
  202. Nth element requires looking through N cons cells, so elements farther
  203. from the beginning of the list take longer to access.  But it is
  204. possible to add elements to the list, or remove elements.
  205.    The following diagram shows the relationship between these types:
  206.                ___________________________________
  207.               |                                   |
  208.               |          Sequence                 |
  209.               |  ______   ______________________  |
  210.               | |      | |                      | |
  211.               | | List | |         Array        | |
  212.               | |      | |  ________   _______  | |
  213.               | |______| | |        | |       | | |
  214.               |          | | Vector | | String| | |
  215.               |          | |________| |_______| | |
  216.               |          |______________________| |
  217.               |___________________________________|
  218.    The elements of vectors and lists may be any Lisp objects.  The
  219. elements of strings are all characters.
  220. * Menu:
  221. * Sequence Functions::    Functions that accept any kind of sequence.
  222. * Arrays::                Characteristics of arrays in Emacs Lisp.
  223. * Array Functions::       Functions specifically for arrays.
  224. * Vectors::               Special characteristics of Emacs Lisp vectors.
  225. * Vector Functions::      Functions specifically for vectors.
  226. File: elisp,  Node: Sequence Functions,  Next: Arrays,  Up: Sequences Arrays Vectors
  227. Sequences
  228. =========
  229.    In Emacs Lisp, a "sequence" is either a list, a vector or a string.
  230. The common property that all sequences have is that each is an ordered
  231. collection of elements.  This section describes functions that accept
  232. any kind of sequence.
  233.  - Function: sequencep OBJECT
  234.      Returns `t' if OBJECT is a list, vector, or string, `nil'
  235.      otherwise.
  236.  - Function: copy-sequence SEQUENCE
  237.      Returns a copy of SEQUENCE.  The copy is the same type of object
  238.      as the original sequence, and it has the same elements in the same
  239.      order.
  240.      Storing a new element into the copy does not affect the original
  241.      SEQUENCE, and vice versa.  However, the elements of the new
  242.      sequence are not copies; they are identical (`eq') to the elements
  243.      of the original.  Therefore, changes made within these elements, as
  244.      found via the copied sequence, are also visible in the original
  245.      sequence.
  246.      If the sequence is a string with text properties, the property
  247.      list in the copy is itself a copy, not shared with the original's
  248.      property list.  However, the actual values of the properties are
  249.      shared.  *Note Text Properties::.
  250.      See also `append' in *Note Building Lists::, `concat' in *Note
  251.      Creating Strings::, and `vconcat' in *Note Vectors::, for others
  252.      ways to copy sequences.
  253.           (setq bar '(1 2))
  254.                => (1 2)
  255.           (setq x (vector 'foo bar))
  256.                => [foo (1 2)]
  257.           (setq y (copy-sequence x))
  258.                => [foo (1 2)]
  259.           
  260.           (eq x y)
  261.                => nil
  262.           (equal x y)
  263.                => t
  264.           (eq (elt x 1) (elt y 1))
  265.                => t
  266.           
  267.           ;; Replacing an element of one sequence.
  268.           (aset x 0 'quux)
  269.           x => [quux (1 2)]
  270.           y => [foo (1 2)]
  271.           
  272.           ;; Modifying the inside of a shared element.
  273.           (setcar (aref x 1) 69)
  274.           x => [quux (69 2)]
  275.           y => [foo (69 2)]
  276.  - Function: length SEQUENCE
  277.      Returns the number of elements in SEQUENCE.  If SEQUENCE is a cons
  278.      cell that is not a list (because the final CDR is not `nil'), a
  279.      `wrong-type-argument' error is signaled.
  280.           (length '(1 2 3))
  281.               => 3
  282.           (length ())
  283.               => 0
  284.           (length "foobar")
  285.               => 6
  286.           (length [1 2 3])
  287.               => 3
  288.  - Function: elt SEQUENCE INDEX
  289.      This function returns the element of SEQUENCE indexed by INDEX.
  290.      Legitimate values of INDEX are integers ranging from 0 up to one
  291.      less than the length of SEQUENCE.  If SEQUENCE is a list, then
  292.      out-of-range values of INDEX return `nil'; otherwise, they trigger
  293.      an `args-out-of-range' error.
  294.           (elt [1 2 3 4] 2)
  295.                => 3
  296.           (elt '(1 2 3 4) 2)
  297.                => 3
  298.           (char-to-string (elt "1234" 2))
  299.                => "3"
  300.           (elt [1 2 3 4] 4)
  301.                error-->Args out of range: [1 2 3 4], 4
  302.           (elt [1 2 3 4] -1)
  303.                error-->Args out of range: [1 2 3 4], -1
  304.      This function generalizes `aref' (*note Array Functions::.) and
  305.      `nth' (*note List Elements::.).
  306. File: elisp,  Node: Arrays,  Next: Array Functions,  Prev: Sequence Functions,  Up: Sequences Arrays Vectors
  307. Arrays
  308. ======
  309.    An "array" object has slots that hold a number of other Lisp
  310. objects, called the elements of the array.  Any element of an array may
  311. be accessed in constant time.  In contrast, an element of a list
  312. requires access time that is proportional to the position of the element
  313. in the list.
  314.    When you create an array, you must specify how many elements it has.
  315. The amount of space allocated depends on the number of elements.
  316. Therefore, it is impossible to change the size of an array once it is
  317. created; you cannot add or remove elements.  However, you can replace an
  318. element with a different value.
  319.    Emacs defines two types of array, both of which are one-dimensional:
  320. "strings" and "vectors".  A vector is a general array; its elements can
  321. be any Lisp objects.  A string is a specialized array; its elements
  322. must be characters (i.e., integers between 0 and 255).  Each type of
  323. array has its own read syntax.  *Note String Type::, and *Note Vector
  324. Type::.
  325.    Both kinds of array share these characteristics:
  326.    * The first element of an array has index zero, the second element
  327.      has index 1, and so on.  This is called "zero-origin" indexing.
  328.      For example, an array of four elements has indices 0, 1, 2, and 3.
  329.    * The elements of an array may be referenced or changed with the
  330.      functions `aref' and `aset', respectively (*note Array
  331.      Functions::.).
  332.    In principle, if you wish to have an array of text characters, you
  333. could use either a string or a vector.  In practice, we always choose
  334. strings for such applications, for four reasons:
  335.    * They occupy one-fourth the space of a vector of the same elements.
  336.    * Strings are printed in a way that shows the contents more clearly
  337.      as characters.
  338.    * Strings can hold text properties.  *Note Text Properties::.
  339.    * Many of the specialized editing and I/O facilities of Emacs accept
  340.      only strings.  For example, you cannot insert a vector of
  341.      characters into a buffer the way you can insert a string.  *Note
  342.      Strings and Characters::.
  343.    By contrast, for an array of keyboard input characters (such as a key
  344. sequence), a vector may be necessary, because many keyboard input
  345. characters are outside the range that will fit in a string.  *Note Key
  346. Sequence Input::.
  347. File: elisp,  Node: Array Functions,  Next: Vectors,  Prev: Arrays,  Up: Sequences Arrays Vectors
  348. Functions that Operate on Arrays
  349. ================================
  350.    In this section, we describe the functions that accept both strings
  351. and vectors.
  352.  - Function: arrayp OBJECT
  353.      This function returns `t' if OBJECT is an array (i.e., either a
  354.      vector or a string).
  355.           (arrayp [a])
  356.           => t
  357.           (arrayp "asdf")
  358.           => t
  359.  - Function: aref ARRAY INDEX
  360.      This function returns the INDEXth element of ARRAY.  The first
  361.      element is at index zero.
  362.           (setq primes [2 3 5 7 11 13])
  363.                => [2 3 5 7 11 13]
  364.           (aref primes 4)
  365.                => 11
  366.           (elt primes 4)
  367.                => 11
  368.           
  369.           (aref "abcdefg" 1)
  370.                => 98           ; `b' is ASCII code 98.
  371.      See also the function `elt', in *Note Sequence Functions::.
  372.  - Function: aset ARRAY INDEX OBJECT
  373.      This function sets the INDEXth element of ARRAY to be OBJECT.  It
  374.      returns OBJECT.
  375.           (setq w [foo bar baz])
  376.                => [foo bar baz]
  377.           (aset w 0 'fu)
  378.                => fu
  379.           w
  380.                => [fu bar baz]
  381.           
  382.           (setq x "asdfasfd")
  383.                => "asdfasfd"
  384.           (aset x 3 ?Z)
  385.                => 90
  386.           x
  387.                => "asdZasfd"
  388.      If ARRAY is a string and OBJECT is not a character, a
  389.      `wrong-type-argument' error results.
  390.  - Function: fillarray ARRAY OBJECT
  391.      This function fills the array ARRAY with OBJECT, so that each
  392.      element of ARRAY is OBJECT.  It returns ARRAY.
  393.           (setq a [a b c d e f g])
  394.                => [a b c d e f g]
  395.           (fillarray a 0)
  396.                => [0 0 0 0 0 0 0]
  397.           a
  398.                => [0 0 0 0 0 0 0]
  399.           (setq s "When in the course")
  400.                => "When in the course"
  401.           (fillarray s ?-)
  402.                => "------------------"
  403.      If ARRAY is a string and OBJECT is not a character, a
  404.      `wrong-type-argument' error results.
  405.    The general sequence functions `copy-sequence' and `length' are
  406. often useful for objects known to be arrays.  *Note Sequence
  407. Functions::.
  408. File: elisp,  Node: Vectors,  Next: Vector Functions,  Prev: Array Functions,  Up: Sequences Arrays Vectors
  409. Vectors
  410. =======
  411.    Arrays in Lisp, like arrays in most languages, are blocks of memory
  412. whose elements can be accessed in constant time.  A "vector" is a
  413. general-purpose array; its elements can be any Lisp objects.  (The other
  414. kind of array in Emacs Lisp is the "string", whose elements must be
  415. characters.)  Vectors in Emacs serve as syntax tables (vectors of
  416. integers), as obarrays (vectors of symbols), and in keymaps (vectors of
  417. commands).  They are also used internally as part of the representation
  418. of a byte-compiled function; if you print such a function, you will see
  419. a vector in it.
  420.    In Emacs Lisp, the indices of the elements of a vector start from
  421. zero and count up from there.
  422.    Vectors are printed with square brackets surrounding the elements.
  423. Thus, a vector whose elements are the symbols `a', `b' and `a' is
  424. printed as `[a b a]'.  You can write vectors in the same way in Lisp
  425. input.
  426.    A vector, like a string or a number, is considered a constant for
  427. evaluation: the result of evaluating it is the same vector.  This does
  428. not evaluate or even examine the elements of the vector.  *Note
  429. Self-Evaluating Forms::.
  430.    Here are examples of these principles:
  431.      (setq avector [1 two '(three) "four" [five]])
  432.           => [1 two (quote (three)) "four" [five]]
  433.      (eval avector)
  434.           => [1 two (quote (three)) "four" [five]]
  435.      (eq avector (eval avector))
  436.           => t
  437. File: elisp,  Node: Vector Functions,  Prev: Vectors,  Up: Sequences Arrays Vectors
  438. Functions That Operate on Vectors
  439. =================================
  440.    Here are some functions that relate to vectors:
  441.  - Function: vectorp OBJECT
  442.      This function returns `t' if OBJECT is a vector.
  443.           (vectorp [a])
  444.                => t
  445.           (vectorp "asdf")
  446.                => nil
  447.  - Function: vector &rest OBJECTS
  448.      This function creates and returns a vector whose elements are the
  449.      arguments, OBJECTS.
  450.           (vector 'foo 23 [bar baz] "rats")
  451.                => [foo 23 [bar baz] "rats"]
  452.           (vector)
  453.                => []
  454.  - Function: make-vector LENGTH OBJECT
  455.      This function returns a new vector consisting of LENGTH elements,
  456.      each initialized to OBJECT.
  457.           (setq sleepy (make-vector 9 'Z))
  458.                => [Z Z Z Z Z Z Z Z Z]
  459.  - Function: vconcat &rest SEQUENCES
  460.      This function returns a new vector containing all the elements of
  461.      the SEQUENCES.  The arguments SEQUENCES may be lists, vectors, or
  462.      strings.  If no SEQUENCES are given, an empty vector is returned.
  463.      The value is a newly constructed vector that is not `eq' to any
  464.      existing vector.
  465.           (setq a (vconcat '(A B C) '(D E F)))
  466.                => [A B C D E F]
  467.           (eq a (vconcat a))
  468.                => nil
  469.           (vconcat)
  470.                => []
  471.           (vconcat [A B C] "aa" '(foo (6 7)))
  472.                => [A B C 97 97 foo (6 7)]
  473.      The `vconcat' function also allows integers as arguments.  It
  474.      converts them to strings of digits, making up the decimal print
  475.      representation of the integer, and then uses the strings instead
  476.      of the original integers.  *Don't use this feature; we plan to
  477.      eliminate it.  If you already use this feature, change your
  478.      programs now!*  The proper way to convert an integer to a decimal
  479.      number in this way is with `format' (*note Formatting Strings::.)
  480.      or `number-to-string' (*note String Conversion::.).
  481.      For other concatenation functions, see `mapconcat' in *Note
  482.      Mapping Functions::, `concat' in *Note Creating Strings::, and
  483.      `append' in *Note Building Lists::.
  484.    The `append' function provides a way to convert a vector into a list
  485. with the same elements (*note Building Lists::.):
  486.      (setq avector [1 two (quote (three)) "four" [five]])
  487.           => [1 two (quote (three)) "four" [five]]
  488.      (append avector nil)
  489.           => (1 two (quote (three)) "four" [five])
  490. File: elisp,  Node: Symbols,  Next: Evaluation,  Prev: Sequences Arrays Vectors,  Up: Top
  491. Symbols
  492. *******
  493.    A "symbol" is an object with a unique name.  This chapter describes
  494. symbols, their components, their property lists, and how they are
  495. created and interned.  Separate chapters describe the use of symbols as
  496. variables and as function names; see *Note Variables::, and *Note
  497. Functions::.  For the precise read syntax for symbols, see *Note Symbol
  498. Type::.
  499.    You can test whether an arbitrary Lisp object is a symbol with
  500. `symbolp':
  501.  - Function: symbolp OBJECT
  502.      This function returns `t' if OBJECT is a symbol, `nil' otherwise.
  503. * Menu:
  504. * Symbol Components::        Symbols have names, values, function definitions
  505.                                and property lists.
  506. * Definitions::              A definition says how a symbol will be used.
  507. * Creating Symbols::         How symbols are kept unique.
  508. * Property Lists::           Each symbol has a property list
  509.                                for recording miscellaneous information.
  510. File: elisp,  Node: Symbol Components,  Next: Definitions,  Prev: Symbols,  Up: Symbols
  511. Symbol Components
  512. =================
  513.    Each symbol has four components (or "cells"), each of which
  514. references another object:
  515. Print name
  516.      The "print name cell" holds a string that names the symbol for
  517.      reading and printing.  See `symbol-name' in *Note Creating
  518.      Symbols::.
  519. Value
  520.      The "value cell" holds the current value of the symbol as a
  521.      variable.  When a symbol is used as a form, the value of the form
  522.      is the contents of the symbol's value cell.  See `symbol-value' in
  523.      *Note Accessing Variables::.
  524. Function
  525.      The "function cell" holds the function definition of the symbol.
  526.      When a symbol is used as a function, its function definition is
  527.      used in its place.  This cell is also used to make a symbol stand
  528.      for a keymap or a keyboard macro, for editor command execution.
  529.      Because each symbol has separate value and function cells,
  530.      variables and function names do not conflict.  See
  531.      `symbol-function' in *Note Function Cells::.
  532. Property list
  533.      The "property list cell" holds the property list of the symbol.
  534.      See `symbol-plist' in *Note Property Lists::.
  535.    The print name cell always holds a string, and cannot be changed.
  536. The other three cells can be set individually to any specified Lisp
  537. object.
  538.    The print name cell holds the string that is the name of the symbol.
  539. Since symbols are represented textually by their names, it is important
  540. not to have two symbols with the same name.  The Lisp reader ensures
  541. this: every time it reads a symbol, it looks for an existing symbol with
  542. the specified name before it creates a new one.  (In GNU Emacs Lisp,
  543. this lookup uses a hashing algorithm and an obarray; see *Note Creating
  544. Symbols::.)
  545.    In normal usage, the function cell usually contains a function or
  546. macro, as that is what the Lisp interpreter expects to see there (*note
  547. Evaluation::.).  Keyboard macros (*note Keyboard Macros::.), keymaps
  548. (*note Keymaps::.) and autoload objects (*note Autoloading::.) are also
  549. sometimes stored in the function cell of symbols.  We often refer to
  550. "the function `foo'" when we really mean the function stored in the
  551. function cell of the symbol `foo'.  We make the distinction only when
  552. necessary.
  553.    The property list cell normally should hold a correctly formatted
  554. property list (*note Property Lists::.), as a number of functions expect
  555. to see a property list there.
  556.    The function cell or the value cell may be "void", which means that
  557. the cell does not reference any object.  (This is not the same thing as
  558. holding the symbol `void', nor the same as holding the symbol `nil'.)
  559. Examining a cell that is void results in an error, such as `Symbol's
  560. value as variable is void'.
  561.    The four functions `symbol-name', `symbol-value', `symbol-plist',
  562. and `symbol-function' return the contents of the four cells of a
  563. symbol.  Here as an example we show the contents of the four cells of
  564. the symbol `buffer-file-name':
  565.      (symbol-name 'buffer-file-name)
  566.           => "buffer-file-name"
  567.      (symbol-value 'buffer-file-name)
  568.           => "/gnu/elisp/symbols.texi"
  569.      (symbol-plist 'buffer-file-name)
  570.           => (variable-documentation 29529)
  571.      (symbol-function 'buffer-file-name)
  572.           => #<subr buffer-file-name>
  573. Because this symbol is the variable which holds the name of the file
  574. being visited in the current buffer, the value cell contents we see are
  575. the name of the source file of this chapter of the Emacs Lisp Manual.
  576. The property list cell contains the list `(variable-documentation
  577. 29529)' which tells the documentation functions where to find the
  578. documentation string for the variable `buffer-file-name' in the `DOC'
  579. file.  (29529 is the offset from the beginning of the `DOC' file to
  580. where that documentation string begins.)  The function cell contains
  581. the function for returning the name of the file.  `buffer-file-name'
  582. names a primitive function, which has no read syntax and prints in hash
  583. notation (*note Primitive Function Type::.).  A symbol naming a
  584. function written in Lisp would have a lambda expression (or a byte-code
  585. object) in this cell.
  586. File: elisp,  Node: Definitions,  Next: Creating Symbols,  Prev: Symbol Components,  Up: Symbols
  587. Defining Symbols
  588. ================
  589.    A "definition" in Lisp is a special form that announces your
  590. intention to use a certain symbol in a particular way.  In Emacs Lisp,
  591. you can define a symbol as a variable, or define it as a function (or
  592. macro), or both independently.
  593.    A definition construct typically specifies a value or meaning for the
  594. symbol for one kind of use, plus documentation for its meaning when used
  595. in this way.  Thus, when you define a symbol as a variable, you can
  596. supply an initial value for the variable, plus documentation for the
  597. variable.
  598.    `defvar' and `defconst' are special forms that define a symbol as a
  599. global variable.  They are documented in detail in *Note Defining
  600. Variables::.
  601.    `defun' defines a symbol as a function, creating a lambda expression
  602. and storing it in the function cell of the symbol.  This lambda
  603. expression thus becomes the function definition of the symbol.  (The
  604. term "function definition", meaning the contents of the function cell,
  605. is derived from the idea that `defun' gives the symbol its definition
  606. as a function.)  `defsubst' and `defalias' are two other ways of
  607. defining a function.  *Note Functions::.
  608.    `defmacro' defines a symbol as a macro.  It creates a macro object
  609. and stores it in the function cell of the symbol.  Note that a given
  610. symbol can be a macro or a function, but not both at once, because both
  611. macro and function definitions are kept in the function cell, and that
  612. cell can hold only one Lisp object at any given time.  *Note Macros::.
  613.    In Emacs Lisp, a definition is not required in order to use a symbol
  614. as a variable or function.  Thus, you can make a symbol a global
  615. variable with `setq', whether you define it first or not.  The real
  616. purpose of definitions is to guide programmers and programming tools.
  617. They inform programmers who read the code that certain symbols are
  618. *intended* to be used as variables, or as functions.  In addition,
  619. utilities such as `etags' and `make-docfile' recognize definitions, and
  620. add appropriate information to tag tables and the
  621. `emacs/etc/DOC-VERSION' file. *Note Accessing Documentation::.
  622. File: elisp,  Node: Creating Symbols,  Next: Property Lists,  Prev: Definitions,  Up: Symbols
  623. Creating and Interning Symbols
  624. ==============================
  625.    To understand how symbols are created in GNU Emacs Lisp, you must
  626. know how Lisp reads them.  Lisp must ensure that it finds the same
  627. symbol every time it reads the same set of characters.  Failure to do
  628. so would cause complete confusion.
  629.    When the Lisp reader encounters a symbol, it reads all the characters
  630. of the name.  Then it "hashes" those characters to find an index in a
  631. table called an "obarray".  Hashing is an efficient method of looking
  632. something up.  For example, instead of searching a telephone book cover
  633. to cover when looking up Jan Jones, you start with the J's and go from
  634. there.  That is a simple version of hashing.  Each element of the
  635. obarray is a "bucket" which holds all the symbols with a given hash
  636. code; to look for a given name, it is sufficient to look through all
  637. the symbols in the bucket for that name's hash code.
  638.    If a symbol with the desired name is found, the reader uses that
  639. symbol.  If the obarray does not contain a symbol with that name, the
  640. reader makes a new symbol and adds it to the obarray.  Finding or adding
  641. a symbol with a certain name is called "interning" it, and the symbol
  642. is then called an "interned symbol".
  643.    Interning ensures that each obarray has just one symbol with any
  644. particular name.  Other like-named symbols may exist, but not in the
  645. same obarray.  Thus, the reader gets the same symbols for the same
  646. names, as long as you keep reading with the same obarray.
  647.    No obarray contains all symbols; in fact, some symbols are not in any
  648. obarray.  They are called "uninterned symbols".  An uninterned symbol
  649. has the same four cells as other symbols; however, the only way to gain
  650. access to it is by finding it in some other object or as the value of a
  651. variable.
  652.    In Emacs Lisp, an obarray is actually a vector.  Each element of the
  653. vector is a bucket; its value is either an interned symbol whose name
  654. hashes to that bucket, or 0 if the bucket is empty.  Each interned
  655. symbol has an internal link (invisible to the user) to the next symbol
  656. in the bucket.  Because these links are invisible, there is no way to
  657. find all the symbols in an obarray except using `mapatoms' (below).
  658. The order of symbols in a bucket is not significant.
  659.    In an empty obarray, every element is 0, and you can create an
  660. obarray with `(make-vector LENGTH 0)'.  *This is the only valid way to
  661. create an obarray.*  Prime numbers as lengths tend to result in good
  662. hashing; lengths one less than a power of two are also good.
  663.    *Do not try to put symbols in an obarray yourself.*  This does not
  664. work--only `intern' can enter a symbol in an obarray properly.  *Do not
  665. try to intern one symbol in two obarrays.*  This would garble both
  666. obarrays, because a symbol has just one slot to hold the following
  667. symbol in the obarray bucket.  The results would be unpredictable.
  668.    It is possible for two different symbols to have the same name in
  669. different obarrays; these symbols are not `eq' or `equal'.  However,
  670. this normally happens only as part of the abbrev mechanism (*note
  671. Abbrevs::.).
  672.      Common Lisp note: In Common Lisp, a single symbol may be interned
  673.      in several obarrays.
  674.    Most of the functions below take a name and sometimes an obarray as
  675. arguments.  A `wrong-type-argument' error is signaled if the name is
  676. not a string, or if the obarray is not a vector.
  677.  - Function: symbol-name SYMBOL
  678.      This function returns the string that is SYMBOL's name.  For
  679.      example:
  680.           (symbol-name 'foo)
  681.                => "foo"
  682.      Changing the string by substituting characters, etc, does change
  683.      the name of the symbol, but fails to update the obarray, so don't
  684.      do it!
  685.  - Function: make-symbol NAME
  686.      This function returns a newly-allocated, uninterned symbol whose
  687.      name is NAME (which must be a string).  Its value and function
  688.      definition are void, and its property list is `nil'.  In the
  689.      example below, the value of `sym' is not `eq' to `foo' because it
  690.      is a distinct uninterned symbol whose name is also `foo'.
  691.           (setq sym (make-symbol "foo"))
  692.                => foo
  693.           (eq sym 'foo)
  694.                => nil
  695.  - Function: intern NAME &optional OBARRAY
  696.      This function returns the interned symbol whose name is NAME.  If
  697.      there is no such symbol in the obarray OBARRAY, `intern' creates a
  698.      new one, adds it to the obarray, and returns it.  If OBARRAY is
  699.      omitted, the value of the global variable `obarray' is used.
  700.           (setq sym (intern "foo"))
  701.                => foo
  702.           (eq sym 'foo)
  703.                => t
  704.           
  705.           (setq sym1 (intern "foo" other-obarray))
  706.                => foo
  707.           (eq sym 'foo)
  708.                => nil
  709.  - Function: intern-soft NAME &optional OBARRAY
  710.      This function returns the symbol in OBARRAY whose name is NAME, or
  711.      `nil' if OBARRAY has no symbol with that name.  Therefore, you can
  712.      use `intern-soft' to test whether a symbol with a given name is
  713.      already interned.  If OBARRAY is omitted, the value of the global
  714.      variable `obarray' is used.
  715.           (intern-soft "frazzle")        ; No such symbol exists.
  716.                => nil
  717.           (make-symbol "frazzle")        ; Create an uninterned one.
  718.                => frazzle
  719.           (intern-soft "frazzle")        ; That one cannot be found.
  720.                => nil
  721.           (setq sym (intern "frazzle"))  ; Create an interned one.
  722.                => frazzle
  723.           (intern-soft "frazzle")        ; That one can be found!
  724.                => frazzle
  725.           (eq sym 'frazzle)              ; And it is the same one.
  726.                => t
  727.  - Variable: obarray
  728.      This variable is the standard obarray for use by `intern' and
  729.      `read'.
  730.  - Function: mapatoms FUNCTION &optional OBARRAY
  731.      This function calls FUNCTION for each symbol in the obarray
  732.      OBARRAY.  It returns `nil'.  If OBARRAY is omitted, it defaults to
  733.      the value of `obarray', the standard obarray for ordinary symbols.
  734.           (setq count 0)
  735.                => 0
  736.           (defun count-syms (s)
  737.             (setq count (1+ count)))
  738.                => count-syms
  739.           (mapatoms 'count-syms)
  740.                => nil
  741.           count
  742.                => 1871
  743.      See `documentation' in *Note Accessing Documentation::, for another
  744.      example using `mapatoms'.
  745.  - Function: unintern SYMBOL &optional OBARRAY
  746.      This function deletes SYMBOL from the obarray OBARRAY.  If
  747.      `symbol' is not actually in the obarray, `unintern' does nothing.
  748.      If OBARRAY is `nil', the current obarray is used.
  749.      If you provide a string instead of a symbol as SYMBOL, it stands
  750.      for a symbol name.  Then `unintern' deletes the symbol (if any) in
  751.      the obarray which has that name.  If there is no such symbol,
  752.      `unintern' does nothing.
  753.      If `unintern' does delete a symbol, it returns `t'.  Otherwise it
  754.      returns `nil'.
  755. File: elisp,  Node: Property Lists,  Prev: Creating Symbols,  Up: Symbols
  756. Property Lists
  757. ==============
  758.    A "property list" ("plist" for short) is a list of paired elements
  759. stored in the property list cell of a symbol.  Each of the pairs
  760. associates a property name (usually a symbol) with a property or value.
  761. Property lists are generally used to record information about a
  762. symbol, such as its documentation as a variable, the name of the file
  763. where it was defined, or perhaps even the grammatical class of the
  764. symbol (representing a word) in a language-understanding system.
  765.    Character positions in a string or buffer can also have property
  766. lists.  *Note Text Properties::.
  767.    The property names and values in a property list can be any Lisp
  768. objects, but the names are usually symbols.  They are compared using
  769. `eq'.  Here is an example of a property list, found on the symbol
  770. `progn' when the compiler is loaded:
  771.      (lisp-indent-function 0 byte-compile byte-compile-progn)
  772. Here `lisp-indent-function' and `byte-compile' are property names, and
  773. the other two elements are the corresponding values.
  774. * Menu:
  775. * Plists and Alists::           Comparison of the advantages of property
  776.                                   lists and association lists.
  777. * Symbol Plists::               Functions to access symbols' property lists.
  778. * Other Plists::                Accessing property lists stored elsewhere.
  779. File: elisp,  Node: Plists and Alists,  Next: Symbol Plists,  Up: Property Lists
  780. Property Lists and Association Lists
  781. ------------------------------------
  782.    Association lists (*note Association Lists::.) are very similar to
  783. property lists.  In contrast to association lists, the order of the
  784. pairs in the property list is not significant since the property names
  785. must be distinct.
  786.    Property lists are better than association lists for attaching
  787. information to various Lisp function names or variables.  If all the
  788. associations are recorded in one association list, the program will need
  789. to search that entire list each time a function or variable is to be
  790. operated on.  By contrast, if the information is recorded in the
  791. property lists of the function names or variables themselves, each
  792. search will scan only the length of one property list, which is usually
  793. short.  This is why the documentation for a variable is recorded in a
  794. property named `variable-documentation'.  The byte compiler likewise
  795. uses properties to record those functions needing special treatment.
  796.    However, association lists have their own advantages.  Depending on
  797. your application, it may be faster to add an association to the front of
  798. an association list than to update a property.  All properties for a
  799. symbol are stored in the same property list, so there is a possibility
  800. of a conflict between different uses of a property name.  (For this
  801. reason, it is a good idea to choose property names that are probably
  802. unique, such as by including the name of the library in the property
  803. name.)  An association list may be used like a stack where associations
  804. are pushed on the front of the list and later discarded; this is not
  805. possible with a property list.
  806. File: elisp,  Node: Symbol Plists,  Next: Other Plists,  Prev: Plists and Alists,  Up: Property Lists
  807. Property List Functions for Symbols
  808. -----------------------------------
  809.  - Function: symbol-plist SYMBOL
  810.      This function returns the property list of SYMBOL.
  811.  - Function: setplist SYMBOL PLIST
  812.      This function sets SYMBOL's property list to PLIST.  Normally,
  813.      PLIST should be a well-formed property list, but this is not
  814.      enforced.
  815.           (setplist 'foo '(a 1 b (2 3) c nil))
  816.                => (a 1 b (2 3) c nil)
  817.           (symbol-plist 'foo)
  818.                => (a 1 b (2 3) c nil)
  819.      For symbols in special obarrays, which are not used for ordinary
  820.      purposes, it may make sense to use the property list cell in a
  821.      nonstandard fashion; in fact, the abbrev mechanism does so (*note
  822.      Abbrevs::.).
  823.  - Function: get SYMBOL PROPERTY
  824.      This function finds the value of the property named PROPERTY in
  825.      SYMBOL's property list.  If there is no such property, `nil' is
  826.      returned.  Thus, there is no distinction between a value of `nil'
  827.      and the absence of the property.
  828.      The name PROPERTY is compared with the existing property names
  829.      using `eq', so any object is a legitimate property.
  830.      See `put' for an example.
  831.  - Function: put SYMBOL PROPERTY VALUE
  832.      This function puts VALUE onto SYMBOL's property list under the
  833.      property name PROPERTY, replacing any previous property value.
  834.      The `put' function returns VALUE.
  835.           (put 'fly 'verb 'transitive)
  836.                =>'transitive
  837.           (put 'fly 'noun '(a buzzing little bug))
  838.                => (a buzzing little bug)
  839.           (get 'fly 'verb)
  840.                => transitive
  841.           (symbol-plist 'fly)
  842.                => (verb transitive noun (a buzzing little bug))
  843. File: elisp,  Node: Other Plists,  Prev: Symbol Plists,  Up: Property Lists
  844. Property Lists Outside Symbols
  845. ------------------------------
  846.    These two functions are useful for manipulating property lists that
  847. are stored in places other than symbols:
  848.  - Function: plist-get PLIST PROPERTY
  849.      This returns the value of the PROPERTY property stored in the
  850.      property list PLIST.  For example,
  851.           (plist-get '(foo 4) 'foo)
  852.                => 4
  853.  - Function: plist-put PLIST PROPERTY VALUE
  854.      This stores VALUE as the value of the PROPERTY property in the
  855.      property list PLIST.  It may modify PLIST destructively, or it may
  856.      construct a new list structure without altering the old.  The
  857.      function returns the modified property list, so you can store that
  858.      back in the place where you got PLIST.  For example,
  859.           (setq my-plist '(bar t foo 4))
  860.                => (bar t foo 4)
  861.           (setq my-plist (plist-put my-plist 'foo 69))
  862.                => (bar t foo 69)
  863.           (setq my-plist (plist-put my-plist 'quux '(a)))
  864.                => (quux (a) bar t foo 5)
  865. File: elisp,  Node: Evaluation,  Next: Control Structures,  Prev: Symbols,  Up: Top
  866. Evaluation
  867. **********
  868.    The "evaluation" of expressions in Emacs Lisp is performed by the
  869. "Lisp interpreter"--a program that receives a Lisp object as input and
  870. computes its "value as an expression".  How it does this depends on the
  871. data type of the object, according to rules described in this chapter.
  872. The interpreter runs automatically to evaluate portions of your
  873. program, but can also be called explicitly via the Lisp primitive
  874. function `eval'.
  875. * Menu:
  876. * Intro Eval::  Evaluation in the scheme of things.
  877. * Eval::        How to invoke the Lisp interpreter explicitly.
  878. * Forms::       How various sorts of objects are evaluated.
  879. * Quoting::     Avoiding evaluation (to put constants in the program).
  880. File: elisp,  Node: Intro Eval,  Next: Eval,  Up: Evaluation
  881. Introduction to Evaluation
  882. ==========================
  883.    The Lisp interpreter, or evaluator, is the program that computes the
  884. value of an expression that is given to it.  When a function written in
  885. Lisp is called, the evaluator computes the value of the function by
  886. evaluating the expressions in the function body.  Thus, running any
  887. Lisp program really means running the Lisp interpreter.
  888.    How the evaluator handles an object depends primarily on the data
  889. type of the object.
  890.    A Lisp object that is intended for evaluation is called an
  891. "expression" or a "form".  The fact that expressions are data objects
  892. and not merely text is one of the fundamental differences between
  893. Lisp-like languages and typical programming languages.  Any object can
  894. be evaluated, but in practice only numbers, symbols, lists and strings
  895. are evaluated very often.
  896.    It is very common to read a Lisp expression and then evaluate the
  897. expression, but reading and evaluation are separate activities, and
  898. either can be performed alone.  Reading per se does not evaluate
  899. anything; it converts the printed representation of a Lisp object to the
  900. object itself.  It is up to the caller of `read' whether this object is
  901. a form to be evaluated, or serves some entirely different purpose.
  902. *Note Input Functions::.
  903.    Do not confuse evaluation with command key interpretation.  The
  904. editor command loop translates keyboard input into a command (an
  905. interactively callable function) using the active keymaps, and then
  906. uses `call-interactively' to invoke the command.  The execution of the
  907. command itself involves evaluation if the command is written in Lisp,
  908. but that is not a part of command key interpretation itself.  *Note
  909. Command Loop::.
  910.    Evaluation is a recursive process.  That is, evaluation of a form may
  911. call `eval' to evaluate parts of the form.  For example, evaluation of
  912. a function call first evaluates each argument of the function call, and
  913. then evaluates each form in the function body.  Consider evaluation of
  914. the form `(car x)': the subform `x' must first be evaluated
  915. recursively, so that its value can be passed as an argument to the
  916. function `car'.
  917.    Evaluation of a function call ultimately calls the function specified
  918. in it.  *Note Functions::.  The execution of the function may itself
  919. work by evaluating the function definition; or the function may be a
  920. Lisp primitive implemented in C, or it may be a byte-compiled function
  921. (*note Byte Compilation::.).
  922.    The evaluation of forms takes place in a context called the
  923. "environment", which consists of the current values and bindings of all
  924. Lisp variables.(1)  Whenever the form refers to a variable without
  925. creating a new binding for it, the value of the binding in the current
  926. environment is used.  *Note Variables::.
  927.    Evaluation of a form may create new environments for recursive
  928. evaluation by binding variables (*note Local Variables::.).  These
  929. environments are temporary and vanish by the time evaluation of the form
  930. is complete.  The form may also make changes that persist; these changes
  931. are called "side effects".  An example of a form that produces side
  932. effects is `(setq foo 1)'.
  933.    The details of what evaluation means for each kind of form are
  934. described below (*note Forms::.).
  935.    ---------- Footnotes ----------
  936.    (1)  This definition of "environment" is specifically not intended
  937. to include all the data that can affect the result of a program.
  938.